package com.frank.service.impl;


import com.frank.dao.InterviewMapper;
import com.frank.dao.InterviewRecordMapper;
import com.frank.dto.CommonListDto;
import com.frank.dto.InterviewDto;
import com.frank.dto.QueryPageDto;
import com.frank.model.Interview;
import com.frank.model.InterviewRecord;
import com.frank.service.InterviewManageService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class InterviewManageServiceImpl implements InterviewManageService{

    @Resource
    private InterviewMapper interviewMapper;

    @Resource
    private InterviewRecordMapper interviewRecordMapper;

    @Override
    public CommonListDto getInterviewList(QueryPageDto queryPageDto) {

        int count = interviewMapper.selectInterviewListCount();

        List<Map> mapList = interviewMapper.selectInterviewList(queryPageDto.getMap(), queryPageDto.getRowBounds());
        List<InterviewDto> interviewDtoList = new ArrayList<>();

        for (Map map : mapList){
            InterviewDto interviewDto = new InterviewDto();
            interviewDto.setInterviewId((Integer) map.get("id"));
            interviewDto.setInterview_name((String)map.get("interview_name"));
            interviewDto.setPlace((String)map.get("place"));
            interviewDto.setRecruit_type((String)map.get("recruit_type"));
            interviewDto.setRemark((String)map.get("remark"));
            interviewDto.setStaffName((String)map.get("staff_name"));

            Date startTime = (Date)map.get("start_time");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            interviewDto.setStart_time(simpleDateFormat.format(startTime));

            interviewDtoList.add(interviewDto);
        }

        return new CommonListDto<>(interviewDtoList,count);
    }

    @Override
    public boolean addInterview(InterviewDto interviewDto) {
        Interview interview = new Interview();
        interview.setInterviewName(interviewDto.getInterview_name());
        interview.setPlace(interviewDto.getPlace());
        interview.setRecruitType(interviewDto.getRecruit_type());
        interview.setStaffId(interviewDto.getStaffId());
        interview.setRemark(interviewDto.getRemark());

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate;
        try {
            startDate = simpleDateFormat.parse(interviewDto.getStart_time());
            interview.setStartTime(startDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        interviewMapper.insertSelective(interview);

        int interviewId = interview.getId();

        for (int userId : interviewDto.getUserId()){
            InterviewRecord interviewRecord = new InterviewRecord();
            interviewRecord.setUserId(userId);
            interviewRecord.setInterviewId(interviewId);
            interviewRecordMapper.insertSelective(interviewRecord);
        }

        return true;
    }

    @Override
    public boolean modifyInterview(Interview interview) {
        return interviewMapper.updateByPrimaryKeySelective(interview) == 1;
    }

    @Override
    public boolean deleteInterview(Integer interviewId) {
        return interviewMapper.deleteByPrimaryKey(interviewId) == 1;
    }
}
